package br.pucrio.inf.les.SPLAnalyser.core.models.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMLResource;

import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Asset;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Feature;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Mapping;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactvisualizermodel.ImpactVisualizerModel;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactvisualizermodel.ImpactvisualizermodelFactory;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactvisualizermodel.ImpactvisualizermodelPackage;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactvisualizermodel.ResultSet;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactvisualizermodel.impl.ImpactvisualizermodelPackageImpl;
import br.pucrio.inf.les.genarch.GenarchEMFPlugin;


public class ImpactVisualizerModelHandler {

	private ImpactvisualizermodelPackage genericvisualizermodelPackage;
	private ImpactvisualizermodelFactory genericvisualizermodelFactory;
	private ImpactVisualizerModel impactVisualizerModel;


	public ImpactVisualizerModelHandler(String name) {				
		init();			
		this.impactVisualizerModel = this.genericvisualizermodelFactory.createImpactVisualizerModel();
		this.impactVisualizerModel.setName(name);
	}

	public ImpactVisualizerModelHandler(ImpactVisualizerModel impactVisualizerModel) {
		init();
		this.impactVisualizerModel = impactVisualizerModel;	
	}

	private void init() {
		ImpactvisualizermodelPackageImpl.init();
		this.genericvisualizermodelPackage = ImpactvisualizermodelPackageImpl.eINSTANCE;
		this.genericvisualizermodelFactory = this.genericvisualizermodelPackage.getImpactvisualizermodelFactory();
	}
	
	public ImpactVisualizerModel createImpactVisualizerModel() {				
		init();			
		this.impactVisualizerModel = this.genericvisualizermodelFactory.createImpactVisualizerModel();
		return impactVisualizerModel;
	}
	
	public ResultSet addResultSet(String name) {		
		ResultSet resultSet = this.genericvisualizermodelFactory.createResultSet();
		resultSet.setName(name);		
		this.impactVisualizerModel.getResults().add(resultSet);
		return resultSet;
	}
	
	public void addFeatureResult(ResultSet resultSet, Feature feature){
		resultSet.getFeatures().add(feature);
	}
	
	public void addFeatureResult(ResultSet resultSet, List<Feature> featureList){
		resultSet.getFeatures().addAll(featureList);
	}
	
	public void addAssetResult(ResultSet resultSet, Asset asset){
		resultSet.getAssets().add(asset);
	}
	
	public void addAssetResult(ResultSet resultSet, List<Asset> assetList){
		resultSet.getAssets().addAll(assetList);
	}

	public void addMappingResult(ResultSet resultSet, Mapping mapping){
		resultSet.getMappings().add(mapping);
	}	
	
	public void addMappingResult(ResultSet resultSet, List<Mapping> mappingList){
		resultSet.getMappings().addAll(mappingList);
	}	
	
	public ImpactVisualizerModel getImpactVisualizerModel() {
		return impactVisualizerModel;
	}

	public void setImpactVisualizerModel(ImpactVisualizerModel impactVisualizerModel) {
		this.impactVisualizerModel = impactVisualizerModel;
	}

	public void saveModel(String path){
		Map options = new HashMap();
		options.put(XMLResource.OPTION_ENCODING, "UTF-8");		
		
		URI modelFileURI = URI.createPlatformResourceURI(path, false);	
		
		ResourceSet analysisModelResourceSet = new ResourceSetImpl();
		Resource impactAnalysisResource = analysisModelResourceSet.createResource(modelFileURI);
		
		impactAnalysisResource.getContents().add(this.getImpactVisualizerModel());

		try {
			impactAnalysisResource.save(options);
		} catch (IOException e) {						
			e.printStackTrace();
			GenarchEMFPlugin.INSTANCE.log(e);
		}	
	}
	
}
